﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Object = System.Object;

public class CardsControl : MonoBehaviour
{
    //简单单例////////////////////////////////////
    public static CardsControl Instance;

    private void Awake()
    {
        Instance = this;
    }
    //简单单例////////////////////////////////////

    //商店父物体
    public GameObject m_storeCards;

    //拥有的卡片父物体
    public GameObject m_myCards;

    //拥有卡片计数
    private int m_myCardsCount = 0;

    //计数已使用N张卡
    private int m_ownCardCount = 0;

    //计数可用骰子次数
    private int m_ownDiceCount = 1;

    //从商店购买卡片放入我的背包
    public void PlaceCard2Package(GameObject card)
    {
        card.transform.parent = m_myCards.transform;
        m_myCardsCount++;
    }

    //从我的背包释放一张卡皮
    public void ReleaseCard(GameObject card)
    {
        m_myCardsCount--;
        if (m_myCardsCount < 0)
        {
            m_myCardsCount = 0;
        }

        Destroy(card);
    }

    public int OwnCardCount
    {
        get { return m_ownCardCount; }
        set
        {
            //大于5次的时候,增加一次骰子机会,并置零
            if (m_ownCardCount == 4)
            {
                m_ownCardCount = 0;
                OwnCardCount = 0;

                OwnDiceCount++;
            }
            else
            {
                m_ownCardCount = value;
            }

            Debug.Log("当前卡片次数:" + m_ownCardCount);
        }
    }

    //骰子数
    public int OwnDiceCount
    {
        get { return m_ownDiceCount; }
        set
        {
            m_ownDiceCount = value;
            //TODO 此处刷新UI界面上骰子的数量
            Debug.Log("当前拥有骰子数量:" + m_ownDiceCount);

            GameObject.Find("Canvas/Panel/Dice/Text").GetComponent<Text>().text = m_ownDiceCount.ToString();
        }
    }


    //洗牌
    public void Presuffling()
    {
        if (m_ownDiceCount < 1)
        {
            return;
        }

        //使用一次骰子数量
        OwnDiceCount--;

        //先清空场上所有卡片, 然后加载出来
        Transform parent = GameObject.Find("Canvas/Panel/S_View/Viewport/Content").transform;

        //清空所有卡片
        for (int i = 0; i < parent.transform.childCount; i++)
        {
            Destroy(parent.GetChild(i).gameObject);
        }

        //重新发卡片
        for (int i = 0; i < 5; i++)
        {
            GameObject obj =
                LoadPrefab.Instance.LoadPrefab2Scenes<GameObject>("card", true, "Canvas/Panel/S_View/Viewport/Content");

            CardsEvent card = obj.GetComponent<CardsEvent>();
            card.m_card = obj;
            card.m_attribute = ReadCard.Instance.RandomMy(BooldControl.Instance.Health, BooldControl.Instance.Mood);
            //Debug.Log(card.m_attribute.Name);

            //代码控制显示器的内容高度
            RectTransform rect = GameObject.Find("Canvas/Panel/S_View/Viewport/Content").GetComponent<RectTransform>();
            rect.offsetMin = new Vector2(rect.offsetMin.x, -5 * 200);
        }
    }
}